package io.config.dbconnect;

import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import interbase.interclient.Driver;
import interbase.interclient.PreparedStatement;
import interbase.interclient.ResultSet;

public class InterBaseConnect {

    
    public static void main(String[] args) {
    	String res = getAll();
    	System.out.println("result: "+res);
	}
    
    public static String getAll() {
        Connection conn = getConn();
        String sql = "select * from areas";
        PreparedStatement pstmt;
        String result = "";
        try {
            pstmt = (PreparedStatement)conn.prepareStatement(sql);
            ResultSet rs = (ResultSet) pstmt.executeQuery();
            int col = rs.getMetaData().getColumnCount();
            System.out.println("============================");
            while (rs.next()) {
                for (int i = 1; i <= col; i++) {
                    try {
                    	result+=new String(rs.getString(i).getBytes("ISO8859_1"),"GBK") + "\t";
						System.out.print(new String(rs.getString(i).getBytes("ISO8859_1"),"GBK") + "\t");
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
                    if ((i == 2) && (rs.getString(i).length() < 8)) {
                        System.out.print("\t");
                    }
                 }
                System.out.println("");
            }
                System.out.println("============================");
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return result;
    }
    
    public static Connection getConn() {
    	String url = "jdbc:interbase://localhost/D:\\java\\db\\database\\MIS101.GDB";
    	
    	try {
//    		Driver d=new com.mysql.jdbc.Driver();
//			System.out.println("d: "+d);
			Class.forName("interbase.interclient.Driver");
			Driver dd=new interbase.interclient.Driver();
			System.out.println("dd: "+dd);
			Connection conn = DriverManager.getConnection( url, "SYSDBA", "masterkey" );
			System.out.println("conn: "+conn);
			return conn;
			
		}catch (ClassNotFoundException e) {
			System.out.println("error: "+e.getMessage());
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
			System.out.println("error: "+e.getMessage());
		}
    	

        System.out.println("mytest: ");
        return null;
    }



}
